package com.yotoodesign.dianba.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yotoodesign.core.util.LoadImageAsynTask;
import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.adapter.SingerSongAdapter;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.BaseData;
import com.yotoodesign.dianba.bean.Singer;
import com.yotoodesign.dianba.bean.Song;
import com.yotoodesign.dianba.custom.xlistview.XListView;
import com.yotoodesign.dianba.custom.xlistview.XListView.IXListViewListener;
import com.yotoodesign.dianba.util.Constant;
import com.yotoodesign.dianba.util.SongService;


public class SingerSongFragment extends BaseFragment  implements IXListViewListener{
	private String title ;
	private XListView lvSongList;
	private Handler mHandler;
	private RelativeLayout singerLayout;
	
	private boolean showUser = true;
	
	private SingerSongAdapter singerSongAdapter;
	
	private Singer singer;
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.singer_song_fragment, container,false);
		TextView tvTitle = (TextView)view.findViewById(R.id.mainTitle);
		TextView tvSingerName = (TextView)view.findViewById(R.id.tvSingerName);
		TextView tvDesc = (TextView)view.findViewById(R.id.tvDesc);
		
		ImageButton back = (ImageButton)view.findViewById(R.id.commonBack);
		back.setOnClickListener(new OnClickListener() {
			FragmentManager fm = getActivity().getSupportFragmentManager();
			@Override
			public void onClick(View v) {
				fm.popBackStack();
			}
		});
		
		singerLayout = (RelativeLayout)view.findViewById(R.id.singerLayout);
		
		if(showUser){
			ImageView ivSingerAvatar = (ImageView)view.findViewById(R.id.ivSingerAvatar);
			LoadImageAsynTask.loadFromUrl(ivSingerAvatar, singer.getImageUrl(), Constant.APP_DIR+"/"+Constant.IMAGE_CACHE);
			
			tvTitle.setText(getString(R.string.singer_desc));
			singerLayout.setVisibility(View.VISIBLE);
		}else{
			tvTitle.setText(title);
			singerLayout.setVisibility(View.GONE);
		}
		
		tvSingerName.setText(title);
		tvDesc.setText(singer.getDesc());
		
		final Button btnOk = (Button)view.findViewById(R.id.btnOk);
		btnOk.setText("");
		btnOk.setBackgroundResource(R.drawable.music_box);
		btnOk.setVisibility(View.VISIBLE);
		btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				FragmentManager fm = getActivity().getSupportFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				ft.detach(SingerSongFragment.this);
				ft.commit();
				
				GlobalApplication.getMainActivity().showMyFragment();
			}
			
		});
		
		
		
		
		GlobalApplication.getMainActivity().showPinYinSearch(View.VISIBLE);
		
		return view;
	}
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		lvSongList = (XListView)getView().findViewById(R.id.lvSongList);
		lvSongList.setPullLoadEnable(true);
		lvSongList.setPullRefreshEnable(false);
		lvSongList.setXListViewListener(this);
		
		singerSongAdapter = new SingerSongAdapter(getActivity());
		singerSongAdapter.setSongList(this.getDataList());
		lvSongList.setAdapter(singerSongAdapter);
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void updateListView() {
		singerSongAdapter.setSongList(getDataList());
		singerSongAdapter.notifyDataSetChanged();
	}

	private void onLoaded() {
		lvSongList.stopRefresh();
		lvSongList.stopLoadMore();
		lvSongList.setRefreshTime("刚刚");
	}
	
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				onLoaded();
			}
		}, Constant.SECOND);
		
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				loadMore();
				onLoaded();
			}
		}, Constant.SECOND);
		
	}
	
	//加载更多数据
	private void loadMore(){
		ArrayList<BaseData> songList = new ArrayList<BaseData>();
		
		List songs = SongService.getInstance().next();
		for(Iterator<Song> it = songs.iterator();it.hasNext();){
			songList.add(it.next());
		}
		singerSongAdapter.getSongList().addAll(songList);
		
	}

	public boolean isShowUser() {
		return showUser;
	}

	public void setShowUser(boolean showUser) {
		this.showUser = showUser;
	}

	public Singer getSinger() {
		return singer;
	}

	public void setSinger(Singer singer) {
		this.singer = singer;
	}
	
}
